home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: uu4news.netcom.com!friend!news
- From: rich@kastle.com (Richard Krehbiel)
- Subject: Re: Syntax for looping through enumerated types
- Message-ID: <1996Jan10.163922.7855@friend.kastle.com>
- Sender: news@friend.kastle.com (News)
- Reply-To: rich@kastle.com
- Organization: Kastle Development Associates
- X-Newsreader: Forte Free Agent 1.0.82
- References: <4cs70o$8ns@ornews.intel.com>
- Date: Wed, 10 Jan 1996 16:41:18 GMT
-
- thurman_b_miller@ccm2.hf.intel.com (Thurman Miller) wrote:
-
- >Suppose I have:
-
- >enum daysoftheweek {MONDAY, TUESDAY, WEDNESDAY,THURSDAY,FRIDAY} ;
-
- >How can I loop through this using a for statement?
-
- >(the following doesn't work)
-
- >daysoftheweek nIndex;
- >for (nIndex=MONDAY;nIndex<=FRIDAY;nIndex++)
- >{
-
- > switch (nIndex):
- > {
- > case MONDAY:
- > doMonday();
- > }
-
- >}
-
- >I'd like to get away from specifying the beginning & end (ie MONDAY &
- >TUESDAY) if possible because my enumerated list will constanty grow
- >and I'd like to only change the definition of the enumerated list and
- >the for statement would continue to work.
-
- >Note: This is just a trivial example, not what I'm actually wanting to
- >do.
-
- Enum values don't have any built-in constraint that says they'll have
- consecutive unique values. You have to ensure this yourself (but it's
- easy).
-
- To iterate through enums that I maintain, I add special "first value"
- and "last value" tags:
-
- enum daysoftheweek {
- FIRST__DAY, MONDAY=FIRST_DAY, TUESDAY, WEDNESDAY, THURSDAY,
- FRIDAY, SATURDAY, SUNDAY, END__DAY };
-
- daysoftheweek nIndex;
-
- for(nIndex = FIRST__DAY; nIndex < END__DAY; nIndex++)
- {
- // etc
- }
-
- You have to keep FIRST__DAY and END__DAY correct, and you can add
- enums between to your heart's content.
-
- --
- Richard Krehbiel, Kastle Systems, Arlington VA USA
- rich@kastle.com (work) or richk@cais.com (personal)
-
-